local skynet = require "skynet"
local filelog = require "filelog"
local filename = "agentcmd.lua"
local service = nil

-----agent cmd function-----
local agentcmd = {
	service = nil
}

function agentcmd.process(session, source, event, ...)
	-- 这段只是为了方便写代码一下，不想每个函数调用前都要加上
	if not service then
		service = agentcmd.service
	end
	local f = agentcmd[event] 
	if f == nil then
		filelog.sys_error(filename.." line22 Agent AgentCMD.process invalid event:"..event)
		return nil
	end
	return f(...)
end

function agentcmd.create_session(conf)
	return service:create_session(conf)
end

-- 这里的代码还有点疑惑。 
function agentcmd.close( ... )
	--延迟释放agent保证agent能正常处理完
	if service.state ~= EGateAgentState.GATE_AGENTSTATE_LOGINED then
		service:agentexit(true)
		skynet.sleep(100)
		service:exit_service()
	end
	return true
end

function agentcmd.disconnect(fd, isheart)
	return service:disconnect(fd, isheart)
end

function agentcmd.reconnect(conf)
	return service:reconnect(conf)
end

function agentcmd.clear_tableaddress()
	return service:clear_tableaddress()
end

function agentcmd.set_tableaddress(table_address)
	return service:set_tableaddress(table_address)
end

function agentcmd.notify_client(msgname, msgbody, retcode, retstr)
	return service:notify_client(msgname, msgbody, retcode, retstr)
end

return agentcmd